{include file="public/header" /}
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-list" data-type="expand">展开</button>
        <button class="layui-btn layuiadmin-btn-list" data-type="fold">折叠</button>
      </div>
      <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
      <script type="text/html" id="table-content-list">
        {{#  if(d.parent_id != 0){ }}
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <!-- <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete"><i class="layui-icon layui-icon-delete"></i>删除</a> -->
        {{#  } else { }}
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">省份禁止操作</a>
        {{#  } }}
      </script>
    </div>
  </div>
</div>
<script>
layui.config({
  base: '__PLUGIN__/treetable-lay/module/'
  ,version: new Date().getTime()
}).extend({
  treetable: 'treetable-lay/treetable',
}).use(['table','form', 'treetable'], function(){
  var $ = layui.jquery;
  var table = layui.table
          , treetable = layui.treetable
          , form = layui.form;
  // 渲染数据
  var renderTable = function () {
    layer.load(2);
    treetable.render({
      elem: '#LAY-app-content-list',
      treeColIndex: 1,
      treeSpid: 0,
      treeIdName: 'id',
      treePidName: 'parent_id',
      treeDefaultClose:true,
      treeLinkage:false,
      url: "{:url('City/getCityList')}",
      cols: [[
        {field: 'id',type: 'checkbox', fixed: 'left'}
        ,{field: 'title_name', title: '区域名称'}
        ,{
          field: 'arealevel', title: '级别', align: 'center', sort: true, totalRow: true, templet: function (d) {
            if (d.arealevel == 1) {
              return '<span class="layui-badge layui-bg-danger">省</span>';
            }
            if (d.arealevel == 2) {
              return '<span class="layui-badge layui-bg-blue">市</span>';
            }
            if (d.arealevel == 3) {
              return '<span class="layui-badge layui-bg-gray">区/县</span>';
            }
          }
        }
        ,{title: '操作', templet: '#table-content-list', align: 'center'}
      ]],
      done: function () {
        layer.closeAll('loading');
      }
    });
  };
  renderTable();

  // 监听工具条
  table.on('tool(LAY-app-content-list)', function(obj){
    var data = obj.data;
    var _id = data.id;
    if(obj.event === 'edit'){
      layer.open({
        type: 2
        ,title: '编辑'
        ,content: "{:url('city/edit')}?id=" + _id
        ,maxmin: true
        ,area: ['60%', '60%']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
                  ,submit = layero.find('iframe').contents().find("#Lay-app-form-edit");
          // 监听提交
          iframeWindow.layui.form.on('submit(Lay-app-form-edit)', function(data){
            var field = data.field;
            $.ajax({
              url:"{:url('city/edit')}",
              type:'post',
              data:{'field':field},
              success:function(res){
                if(res.code == 0){
                  layer.msg(res.msg,{icon:1});
                  table.reload('LAY-app-content-list');
                  layer.close(index);
                } else{
                  return layer.msg(res.msg,{icon:5});
                }
              }
            });
          });
          submit.trigger('click');
        }
      });
    }
  });

  var $ = layui.$, active = {
    insert: function(){
      layer.open({
        type: 2
        ,title: '添加城市'
        ,content: "{:url('City/insert')}"
        ,maxmin: true
        ,area: ['400px', '300px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
                  ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-insert");
          // 监听提交
          iframeWindow.layui.form.on('submit(layuiadmin-app-form-insert)', function(data){
            var field = data.field; //获取提交的字段
            if(field.name == ''){ layer.msg('行业名称不能为空不能为空'); return false;}
            $.ajax({
              url:"{:url('City/insert')}",
              type:'post',
              data:{'field':field},
              success:function(res){
                if(res.code == 1){
                  layer.msg(res.msg,{icon:1});
                } else{
                  layer.msg(res.msg,{icon:5});
                  return false;
                }
              }
            });
            layer.close(index);
            renderTable();
          });
          submit.trigger('click');
        }
      });
    },
    expand: function(){
      treetable.expandAll('#LAY-app-content-list');
    },
    fold: function(){
      treetable.foldAll('#LAY-app-content-list');
    },
  };
  // 监听 data-type 事件
  $('.layui-btn.layuiadmin-btn-list').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
</script>
{include file="public/footer" /}